﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Dora
{
    public partial class SearchResultForm : Form
    {
        private bool extendedView = false;        

        public SearchResultForm(List<KeyValuePair<double, Core.SearchResult>> results, string searchString)
        {
            InitializeComponent();           
            tbQuery.Text = searchString;                     
            wbResults.DocumentText = ConvertToHtmlTable(results);            
        }      

        private static string ConvertToHtmlTable(List<KeyValuePair<double, Core.SearchResult>> results)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<html><head><STYLE TYPE=\"text/css\"><!--TD{font-size: 16pt;}---></STYLE></head><body><table cellspacing=2 cellpadding=2 border=1 width=\"100%\">");
            for (int i = 0; i < results.Count; i++)
            {
                string backgroundColor, goodResultFlag, siteName;
                if (results[i].Value.Url.Contains("stackoverflow.com")) siteName = "Stack overflow";
                else if (results[i].Value.Url.Contains("daniweb.com")) siteName = "Daniweb";
                else if (results[i].Value.Url.Contains("codeguru.com")) siteName = "Codeguru";
                else if (results[i].Value.Url.Contains("devshed.com")) siteName = "Dev Shed";
                else if (results[i].Value.Url.Contains("bytes.com")) siteName = "Bytes";
                else throw new Exception("Cannot resolve site name");
                if (results[i].Key >= 3.15) { backgroundColor = /*"#c7ffce"*//*"#a0ffac"*/ "#79ff8a"; goodResultFlag = "*"; }
                else if (results[i].Key >= 2.40) { backgroundColor = "white"; goodResultFlag = "&nbsp;"; }
                else continue;
                builder.AppendFormat("<tr bgcolor=\"{0}\">", backgroundColor); 
                builder.AppendFormat("<td rowspan=2 width=5px><b>{0}</b></td>", goodResultFlag);
                builder.AppendFormat("<td rowspan=2 width=10px>{0}</td>", siteName);
                builder.AppendFormat("<td>{0}</td>", results[i].Value.Title.Replace("<", "&lt;").Replace(">", "&gt;"));
                builder.AppendFormat("<td width=50px rowspan=2 align=center><a href=\"{0}\">link</a></td>", results[i].Value.Url);
                builder.Append("</tr>");
                builder.AppendFormat("<tr bgcolor=\"{0}\"><td>{1}</td></tr>", backgroundColor, results[i].Value.Preview.Replace("<", "&lt;").Replace(">", "&gt;"));
                builder.Append("<tr></tr><tr></tr>");
            }
            builder.Append("</table></body></html>");
            return builder.ToString();
        }

        private void bRunSearch_Click(object sender, EventArgs e)
        {          
            var res = Core.Analyzer.MakeRequest(null, null, "", null, tbQuery.Text);            
            DisplayHtml(ConvertToHtmlTable(res)); 
        }

        private void DisplayHtml(string html) 
        {
            wbResults.Navigate("about:blank");
            if (wbResults.Document != null)
            {
                wbResults.Document.Write(string.Empty);
            }
            wbResults.DocumentText = html;
        }

        private void wbResults_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (e.Url.ToString() != "about:blank") 
            {
                e.Cancel = true;
                var tp = new TabPage();
                tcResults.SuspendLayout();
                tp.SuspendLayout();
                tp.Name = string.Format("tpWindow{0}", tcResults.TabCount + 1);
                tp.Text = string.Format("Window{0}", tcResults.TabCount + 1);
                tp.TabIndex = tcResults.TabCount;
                var wb = new WebBrowser();
                wb.Name = string.Format("wbWindow{0}", tcResults.TabCount + 1);
                wb.Navigate(e.Url);
                wb.AllowNavigation = false;
                wb.Dock = DockStyle.Fill;
                tp.Controls.Add(wb);
                tcResults.Controls.Add(tp);
                tp.ResumeLayout();
                tcResults.ResumeLayout();
                Invalidate(true);
            }
        }

        private void tcResults_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenuStrip1.Show(tcResults, e.Location);
            }
        }

        private void removeTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tcResults.SelectedIndex == 0) 
            { 
                MessageBox.Show("You cannot remove Result list tab", Text, MessageBoxButtons.OK); 
                return; 
            }
            var st = tcResults.SelectedTab;
            tcResults.Controls.Remove(st);
            st.Dispose();
        }         
    }
}
